﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace FTP客户端
{
    public partial class LoginWindow : Form
    {
        public string ftpServerIP;
        public LoginWindow()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void LoginWindow_Load(object sender, EventArgs e)
        {

        }

        private void serverIPLabel_Click(object sender, EventArgs e)
        {

        }

        private void LoginButton_Click(object sender, EventArgs e)
        {
            string ipPattern = @"^(\d{1,3}\.){3}\d{1,3}$";
            if (!Regex.IsMatch(serverIPTextBox.Text, ipPattern))
            {
                MessageBox.Show("请输入有效的IP地址");
                return;
            }
            ftpServerIP = serverIPTextBox.Text;
            try
            {
                FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create($"ftp://{ftpServerIP}");
                // 设置为匿名登录，用户名是"anonymous"，密码可以是任意字符串，这里使用"anonymous@example.com"
                request.Credentials = new NetworkCredential("anonymous", "anonymous@example.com");
                request.Method = WebRequestMethods.Ftp.ListDirectory;
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                response.Close();
                MessageBox.Show("连接成功");
                MainWindow mainWindow = new MainWindow(this);
                mainWindow.Show();
                this.Hide();
            }
            catch (WebException ex)
            {
                MessageBox.Show($"连接失败: {ex.Message}");
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            AnotherForm anotherForm = new AnotherForm(this);
            anotherForm.Show();
            this.Hide();
        }
    }
}
